package com.moon.game.profile;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.mongodb.Mongo;
import com.moon.game.database.MongoUtil;

public class ProfileConfig {
//	private static int profileNum = 1;
	private static Document document = null;
	public static Element root = null;
	public static Element profile = null;
	public static Element item = null;
	public static Element playerItem = null;
	public static Map<Integer, Mongo> playerProfile = new HashMap<Integer, Mongo>(); // Profile
	public static Map<Integer, Mongo> itemsProfile = new HashMap<Integer, Mongo>();
	public static Map<Integer, Mongo> playerItemsProfile = new HashMap<Integer, Mongo>();
	
	//TODO:以后要修改成主从读写分离，写-->主，读-->从
	public static void init(String file) throws Exception{
		SAXReader reader = new SAXReader();
		document = reader.read(new File(file));
		root = document.getRootElement();
		if (root == null) {
			throw new Exception("no root node in config");
		}
		profile = root.element("profiles");
		if (profile == null) {
			throw new Exception("no profiles node in config");
		}
		@SuppressWarnings("unchecked")
		List<Element> playerServers = profile.elements("mongo");
		for (Iterator<Element> it = playerServers.iterator(); it.hasNext();) {
			Element elm = it.next();
			String ip = elm.attributeValue("host");
			int port = Integer.parseInt(elm.attributeValue("port"));
			playerProfile.put(Integer.parseInt(elm.attributeValue("id")), MongoUtil.createMongo(ip, port));
		}
		
		item = root.element("items");
		if (item == null) {
			throw new Exception("no items node in config");
		}
		@SuppressWarnings("unchecked")
		List<Element> itemServers = profile.elements("mongo");
		for (Iterator<Element> it = itemServers.iterator(); it.hasNext();) {
			Element elm = it.next();
			String ip = elm.attributeValue("host");
			int port = Integer.parseInt(elm.attributeValue("port"));
			itemsProfile.put(Integer.parseInt(elm.attributeValue("id")), MongoUtil.createMongo(ip, port));
		}
		
		playerItem = root.element("player_items");
		if (playerItem == null) {
			throw new Exception("no items node in config");
		}
		@SuppressWarnings("unchecked")
		List<Element> playerItemServers = profile.elements("mongo");
		for (Iterator<Element> it = playerItemServers.iterator(); it.hasNext();) {
			Element elm = it.next();
			String ip = elm.attributeValue("host");
			int port = Integer.parseInt(elm.attributeValue("port"));
			playerItemsProfile.put(Integer.parseInt(elm.attributeValue("id")), MongoUtil.createMongo(ip, port));
		}
		
	}
	
	/**
	 * 根据编号取playerProfile
	 * 
	 * @param id
	 * @return
	 */
	public static Mongo getPlayerProfile(int id) {
		return playerProfile.get(id);
	}
	
	/**
	 * 人物profile数量
	 * 
	 * @return
	 */
	public static int getPlayerProfileNum() {
		return playerProfile.size();
	}
	
	/**
	 * 根据id获得itemsProfile
	 * 
	 * @param id
	 * @return
	 */
	public static Mongo getItemsProfile(int id) {
		return itemsProfile.get(id);
	}
	
	/**
	 * 道具profile数量
	 * 
	 * @return
	 */
	public static int getItemsProfileNum() {
		return itemsProfile.size();
	}
	
	/**
	 * 根据id获得itemsProfile
	 * 
	 * @param id
	 * @return
	 */
	public static Mongo getPlayerItemsProfile(int id) {
		return playerItemsProfile.get(id);
	}
	
	/**
	 * 道具profile数量
	 * 
	 * @return
	 */
	public static int getPlayerItemsProfileNum() {
		return playerItemsProfile.size();
	}
}
